﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using UnityEngine;
using Item;

namespace ItemComponent
{
    public static class MyComponentFactory
    {
        public static IMyItemComponent AddMyComponent(this ItemBase item, STMyComponetData componetData)
        {
            if (item == null) return null;

            IMyItemComponent itemComponent = null;

            Type type = Type.GetType(componetData.strType);
            if (type == null || !typeof(IMyItemComponent).IsAssignableFrom(type))
            {
                //热更部分
                itemComponent = ILRuntime.ILRuntimeTools.GetHotFixObj(componetData.strType) as IMyItemComponent;
                if (itemComponent == null)
                {
                    Debug.LogError("本体跟热更都没有该类型 --- type=" + componetData.strType);
                    return null;
                }
            }
            else
            {
                //本体部分
                if (typeof(MonoBehaviour).IsAssignableFrom(type))
                    itemComponent = item.gameObject.AddComponent(type) as IMyItemComponent;
                else
                    itemComponent = Activator.CreateInstance(type) as IMyItemComponent;
            }

            itemComponent.InitComponent(componetData.data, item);
            item.RegistComponent(itemComponent);
            return itemComponent;
        }
    }
}
